<script setup lang="ts">
import type { AddressItem } from '@/types'
import { ref, computed } from 'vue'

interface Props {
  list: AddressItem[]
}
const props = defineProps<Props>()

interface Emits {
  (e: 'close', address?: AddressItem): void
}
const emit = defineEmits<Emits>()

const activeIndex = ref(0)
const currentAddress = computed(() => props.list[activeIndex.value])
// 更换选中的收货地址
const onActiveIndexChange = (index: number) => {
  if (activeIndex.value === index) {
    activeIndex.value = -1
  } else {
    activeIndex.value = index
  }
}
</script>

<template>
  <view class="address-panel">
    <!-- 关闭按钮 -->
    <text class="close icon-close" @tap="emit('close')"></text>
    <!-- 标题 -->
    <view class="title">配送至</view>
    <!-- 内容 -->
    <view class="content">
      <view
        class="item"
        v-for="(item, index) in list"
        :key="item.id"
        @tap="onActiveIndexChange(index)"
      >
        <view class="user"
          >{{ item.receiver }} {{ item.contact }}
          <text v-if="item.isDefault" class="badge">默认</text></view
        >
        <view class="address">{{ item.fullLocation }} {{ item.address }}</view>
        <text :class="['icon', activeIndex === index ? 'icon-checked' : 'icon-ring']"></text>
      </view>
    </view>
    <view class="footer">
      <navigator
        :class="['button', 'secondary', { add: activeIndex >= 0 }]"
        hover-class="none"
        url="/pagesMember/address-form/address-form"
      >
        新建地址
      </navigator>
      <view
        v-show="activeIndex >= 0"
        class="button primary confirm"
        @tap="emit('close', currentAddress)"
        >确定</view
      >
    </view>
  </view>
</template>

<style lang="scss">
.address-panel {
  position: relative;
  padding: 0 30rpx;
  border-radius: 10rpx 10rpx 0 0;
  background-color: #fff;
}

.title {
  padding: 40rpx 0;
  font-size: 32rpx;
  font-weight: normal;
  color: #444;
  text-align: center;
  line-height: 1;
  border-bottom: 1rpx solid #ddd;
}

.close {
  position: absolute;
  top: 24rpx;
  right: 24rpx;
}

.content {
  padding: 20rpx;
  min-height: 300rpx;
  max-height: 540rpx;
  overflow: auto;

  &::-webkit-scrollbar {
    display: none;
    width: 0;
    height: 0;
    color: transparent;
  }

  .item {
    position: relative;
    padding: 30rpx 50rpx 30rpx 60rpx;
    background-image: url(https://pcapi-xiaotuxian-front-devtest.itheima.net/miniapp/images/locate.png);
    background-size: 40rpx;
    background-repeat: no-repeat;
    background-position: 0 center;
  }

  .icon {
    position: absolute;
    top: 50%;
    right: 0;
    transform: translateY(-50%);
    font-size: 40rpx;
    color: #999;
  }

  .icon-checked {
    color: #27ba9b;
  }

  .icon-ring {
    color: #444;
  }

  .user {
    font-size: 28rpx;
    font-weight: 500;
    color: #444;

    .badge {
      display: inline-block;
      margin: 2rpx 0 0 10rpx;
      padding: 4rpx 10rpx 2rpx 14rpx;
      font-size: 26rpx;
      color: #27ba9b;
      border-radius: 6rpx;
      border: 1rpx solid #27ba9b;
    }
  }

  .address {
    font-size: 26rpx;
    color: #666;
  }
}

.footer {
  display: flex;
  justify-content: space-between;
  padding: 20rpx 0 40rpx;
  font-size: 28rpx;
  color: #444;

  /* #ifdef H5 || APP-PLUS */
  /* 兼容H5端和App端的导航链接样式 */
  .navigator-wrap {
    flex: 1;
  }
  /* #endif */

  .button {
    flex: 1;
    // margin: 0 20rpx;
    height: 72rpx;
    color: #fff;
    text-align: center;
    line-height: 72rpx;
    border-radius: 72rpx;
  }

  .add {
    border-radius: 72rpx 0 0 72rpx;
  }

  .confirm {
    border-radius: 0 72rpx 72rpx 0;
  }

  .primary {
    color: #fff;
    background-color: #27ba9b;
  }

  .secondary {
    background-color: #ffa868;
  }
}
</style>
